<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>kotlin-面向对象 | litton ishir</title>
    <meta name="description" content="&quot;Someday&quot;sounds a lot like the thing people say when they actually mean &quot;Never&quot;. ">
    <link rel="icon" href="https://upload-images.jianshu.io/upload_images/1074123-dc819e68a5f3c3f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
    
    <link rel="preload" href="/assets/css/0.styles.956f79c0.css" as="style"><link rel="preload" href="/assets/js/app.b387b3a3.js" as="script"><link rel="preload" href="/assets/js/6.0cda6858.js" as="script"><link rel="prefetch" href="/assets/js/10.89f8a53a.js"><link rel="prefetch" href="/assets/js/11.79be8c9b.js"><link rel="prefetch" href="/assets/js/2.abd3539a.js"><link rel="prefetch" href="/assets/js/3.6b6c4ca0.js"><link rel="prefetch" href="/assets/js/4.93ebd6e9.js"><link rel="prefetch" href="/assets/js/5.9a8427c2.js"><link rel="prefetch" href="/assets/js/7.905ce75a.js"><link rel="prefetch" href="/assets/js/8.12136322.js"><link rel="prefetch" href="/assets/js/9.f57e2ac2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.956f79c0.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="container"><div id="head-c"><div class="row"><div class="offset-lg-2 col-lg-8 offset-md-1 col-md-10"><h1 id="blog-name"><a href="/" class="router-link-active">
                litton ishir
            </a></h1> <a href="https://weibo.com/littonishir" target="_blank" class="social"><i class="fa fa-fw fa-weibo"></i></a><a href="https://github.com/littonishir" target="_blank" class="social"><i class="fa fa-fw fa-github"></i></a><a href="https://space.bilibili.com/268398214/" target="_blank" class="social"><i class="fa fa-fw fa-terminal"></i></a><a href="https://www.instagram.com/littonishir/" target="_blank" class="social"><i class="fa fa-fw fa-instagram"></i></a></div></div></div> <div class="row"><div class="offset-lg-2 col-lg-8 offset-md-1 col-md-10"><div><article><p class="post-page-meta">Mar 27, 2017</p> <hr> <div class="content"><h1 id="kotlin-面向对象"><a href="#kotlin-面向对象" aria-hidden="true" class="header-anchor">#</a> kotlin-面向对象</h1> <p>在Java中有继承 封装 和 多态 那么在Kotlin中自然也是有的.这里不再累述</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token comment">//继承</span>
<span class="token keyword">class</span> <span class="token function">MyDrived</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">MyBase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  
    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">method1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>  
<span class="token punctuation">}</span>  
</code></pre></div><br> <h3 id="类及其成员的可见性"><a href="#类及其成员的可见性" aria-hidden="true" class="header-anchor">#</a> 类及其成员的可见性</h3> <br> <ul><li>public	 : 均可见</li> <li>private	 : 只用类内部可见</li> <li>protected	 : 子类可见</li> <li>internal	 : 模块内可见(model内可见)</li></ul> <br> <h4 id="object"><a href="#object" aria-hidden="true" class="header-anchor">#</a> object</h4> <br> <ul><li>只有一个实例的类</li> <li>不能自定义构造方法</li> <li>可以实现接口,继承父类</li> <li>本质上就是单利模式的最基本实现</li></ul> <br> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> Driver

<span class="token keyword">interface</span> OnExternalDriverMountListener <span class="token punctuation">{</span>
    <span class="token keyword">fun</span> <span class="token function">onMount</span><span class="token punctuation">(</span>driver<span class="token operator">:</span> Driver<span class="token punctuation">)</span>

    <span class="token keyword">fun</span> <span class="token function">onUnmount</span><span class="token punctuation">(</span>driver<span class="token operator">:</span> Driver<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">abstract</span> <span class="token keyword">class</span> Player

<span class="token keyword">object</span> MusicPlayer <span class="token operator">:</span> <span class="token function">Player</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> OnExternalDriverMountListener <span class="token punctuation">{</span>
    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">onMount</span><span class="token punctuation">(</span>driver<span class="token operator">:</span> Driver<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">onUnmount</span><span class="token punctuation">(</span>driver<span class="token operator">:</span> Driver<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token keyword">val</span> state<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">0</span>

    <span class="token keyword">fun</span> <span class="token function">play</span><span class="token punctuation">(</span>url<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token keyword">fun</span> <span class="token function">stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="伴生对象"><a href="#伴生对象" aria-hidden="true" class="header-anchor">#</a> 伴生对象</h3> <br> <ul><li>伴生对象与Java中的静态方法类似</li> <li>每个类可以对应一个伴生对象</li> <li>伴生对象的成员全局只有一份</li></ul> <p><strong>companion object</strong></p> <p>Kotlin中使用方式</p> <ol><li>可以通过 类名.函数名 调用函数</li> <li>那么变量 类名.变量名 调用变量</li></ol> <p>Java中若想调用</p> <ol><li>在函数上加 @JvmStatic 注解</li> <li>在变量上加 @JvmField  注解 既可以正常调用</li></ol> <p>伴生对象的写法如下:</p> <div class="language-Kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token operator">&lt;</span>String<span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span>Girl<span class="token punctuation">.</span><span class="token function">sing</span><span class="token punctuation">(</span><span class="token string">&quot;来相思树下&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token function">println</span><span class="token punctuation">(</span>Girl<span class="token punctuation">.</span>TAG<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> Girl <span class="token keyword">private</span> <span class="token keyword">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">companion</span> <span class="token keyword">object</span> <span class="token punctuation">{</span>
        <span class="token annotation builtin">@JvmStatic</span>
        <span class="token keyword">fun</span> <span class="token function">sing</span><span class="token punctuation">(</span>song<span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token operator">:</span> String <span class="token punctuation">{</span>

            <span class="token keyword">return</span> song
        <span class="token punctuation">}</span>

        <span class="token annotation builtin">@JvmStatic</span>
        <span class="token keyword">fun</span> <span class="token function">dance</span><span class="token punctuation">(</span>dance<span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token operator">:</span> String <span class="token punctuation">{</span>
            <span class="token keyword">return</span> dance
        <span class="token punctuation">}</span>

        <span class="token annotation builtin">@JvmField</span>
        <span class="token keyword">val</span> TAG<span class="token operator">:</span> String <span class="token operator">=</span> <span class="token string">&quot;涂山苏苏&quot;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="幕后字段"><a href="#幕后字段" aria-hidden="true" class="header-anchor">#</a> 幕后字段</h3> <p>kotlin没有显示字段,所有为字段赋值的操作都会调用set方法,对于一些必须要直接赋值的操作提供了幕后字段.</p> <p><strong>field</strong></p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>        <span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">8</span>
        <span class="token keyword">set</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>  <span class="token punctuation">{</span>
            <span class="token function">println</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
            field<span class="token operator">=</span>value
        <span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="扩展成员"><a href="#扩展成员" aria-hidden="true" class="header-anchor">#</a> 扩展成员</h3> <p>可以对Java已有的类进行扩展成员</p> <p>比如我想对String类添加一个方法 对传入字符串打印N次</p> <ol><li>操作符扩展方法  字符串*3</li> <li>普通方法名扩展方法  字符串.times(3)</li></ol> <div class="language-Kotlin extra-class"><pre class="language-kotlin"><code> <span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token operator">&lt;</span>String<span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;lalala&quot;</span> <span class="token operator">*</span> <span class="token number">3</span><span class="token punctuation">)</span>
    <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;lalala&quot;</span><span class="token punctuation">.</span><span class="token function">times</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token punctuation">}</span>
 <span class="token keyword">operator</span> <span class="token keyword">fun</span> String<span class="token punctuation">.</span><span class="token function">times</span><span class="token punctuation">(</span>time<span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token operator">:</span> String <span class="token punctuation">{</span>
            <span class="token keyword">val</span> stringBuilder <span class="token operator">=</span> <span class="token function">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token keyword">in</span> <span class="token number">0</span> until time<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                stringBuilder<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token operator">+</span> <span class="token string">&quot;\n&quot;</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> stringBuilder<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="属性代理"><a href="#属性代理" aria-hidden="true" class="header-anchor">#</a> 属性代理</h3> <p>使用方法 : val/var &lt;属性名&gt;:&lt;Type&gt; by &lt;代理对象&gt;</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> hello2 <span class="token keyword">by</span> <span class="token function">X</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">//若是可推导出类型可不写</span>
</code></pre></div><p>自定义代理时要实现相应的setValue/getValue方法</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> Delegates<span class="token punctuation">{</span>
    <span class="token keyword">val</span> hello <span class="token keyword">by</span> lazy <span class="token punctuation">{</span>
        <span class="token string">&quot;HelloWorld&quot;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">val</span> hello2 <span class="token keyword">by</span> <span class="token function">X</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">var</span> hello3 <span class="token keyword">by</span> <span class="token function">X</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> X<span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">var</span> value<span class="token operator">:</span> String<span class="token operator">?</span> <span class="token operator">=</span> <span class="token keyword">null</span>

    <span class="token keyword">operator</span> <span class="token keyword">fun</span> <span class="token function">getValue</span><span class="token punctuation">(</span>thisRef<span class="token operator">:</span> Any<span class="token operator">?</span><span class="token punctuation">,</span> property<span class="token operator">:</span> KProperty<span class="token operator">&lt;</span><span class="token operator">*</span><span class="token operator">&gt;</span><span class="token punctuation">)</span><span class="token operator">:</span> String <span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;getValue: <span class="token interpolation variable">$thisRef</span> -&gt; <span class="token interpolation"><span class="token delimiter variable">${</span>property<span class="token punctuation">.</span>name<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> value<span class="token operator">?:</span> <span class="token string">&quot;&quot;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">operator</span> <span class="token keyword">fun</span> <span class="token function">setValue</span><span class="token punctuation">(</span>thisRef<span class="token operator">:</span> Any<span class="token operator">?</span><span class="token punctuation">,</span> property<span class="token operator">:</span> KProperty<span class="token operator">&lt;</span><span class="token operator">*</span><span class="token operator">&gt;</span><span class="token punctuation">,</span> value<span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;setValue, <span class="token interpolation variable">$thisRef</span> -&gt; <span class="token interpolation"><span class="token delimiter variable">${</span>property<span class="token punctuation">.</span>name<span class="token delimiter variable">}</span></span> = <span class="token interpolation variable">$value</span>&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token operator">&lt;</span>String<span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> delegates <span class="token operator">=</span> <span class="token function">Delegates</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">println</span><span class="token punctuation">(</span>delegates<span class="token punctuation">.</span>hello<span class="token punctuation">)</span>
    <span class="token function">println</span><span class="token punctuation">(</span>delegates<span class="token punctuation">.</span>hello2<span class="token punctuation">)</span>
    <span class="token function">println</span><span class="token punctuation">(</span>delegates<span class="token punctuation">.</span>hello3<span class="token punctuation">)</span>
    delegates<span class="token punctuation">.</span>hello3 <span class="token operator">=</span> <span class="token string">&quot;value of hello3&quot;</span>
    <span class="token function">println</span><span class="token punctuation">(</span>delegates<span class="token punctuation">.</span>hello3<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="数据类"><a href="#数据类" aria-hidden="true" class="header-anchor">#</a> 数据类</h3> <p>数据类用data关键字标记</p> <p><strong>data</strong></p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">data</span> <span class="token keyword">class</span> <span class="token function">User</span><span class="token punctuation">(</span><span class="token keyword">val</span> name<span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token keyword">val</span> age<span class="token operator">:</span> Int<span class="token punctuation">)</span>
</code></pre></div><p>数据类须满足以下要求:</p> <ul><li>主构造函数需要至少有一个参数 ;</li> <li>主构造函数的所有参数需要标记为 val 或 var ;</li> <li>数据类不能是抽象\开放\密封或者内部的;</li> <li>在1.1之前数据类只能实现接口.自 1.1 起,数据类可以扩展其他类(示例请参见密封类).</li></ul> <p>在 JVM 中,如果生成的类需要含有一个无参的构造函数,则所有的属性必须指定默认值.</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">data</span> <span class="token keyword">class</span> <span class="token function">User</span><span class="token punctuation">(</span><span class="token keyword">val</span> name<span class="token operator">:</span> String <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token keyword">val</span> age<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span>
</code></pre></div><p>补充:data class 是被final修饰的不可以被继承,而且没有无参构造,这就导致它无法作为javabean使用.官方出了两个插件来解决这个问题.</p> <ul><li><ol><li>Projec gradle添加依赖</li></ol></li></ul> <div class="language-gradle extra-class"><pre class="language-text"><code> dependencies {
        classpath&quot;org.jetbrains.kotlin:kotlin-allopen:$kotlin_version&quot;
        classpath &quot;org.jetbrains.kotlin:kotlin-noarg:$kotlin_version&quot;
    }
</code></pre></div><ul><li><ol start="2"><li>创建自己的annotation</li></ol></li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">package</span> com<span class="token punctuation">.</span>littonishir<span class="token punctuation">.</span>github<span class="token punctuation">.</span>annotation
<span class="token keyword">annotation</span> <span class="token keyword">class</span> Ishir
</code></pre></div><ul><li><ol start="3"><li>Module gradle应用插件</li></ol></li></ul> <div class="language-gradle extra-class"><pre class="language-text"><code>apply plugin: 'kotlin-noarg'
apply plugin: 'kotlin-allopen'

noArg{
    annotation(&quot;com.littonishir.github.annotation.Ishir&quot;)
}

allOpen{
    annotation(&quot;com.littonishir.github.annotation.Ishir&quot;)
}

</code></pre></div><ul><li><ol start="4"><li>使用自己定义的annotation</li></ol></li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">package</span> com<span class="token punctuation">.</span>littonishir<span class="token punctuation">.</span>github

<span class="token keyword">import</span> com<span class="token punctuation">.</span>littonishir<span class="token punctuation">.</span>github<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Ishir

<span class="token annotation builtin">@Ishir</span>
<span class="token keyword">data</span> <span class="token keyword">class</span> <span class="token function">Games</span><span class="token punctuation">(</span><span class="token keyword">val</span> name<span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token keyword">val</span> version<span class="token operator">:</span> String<span class="token punctuation">)</span>
</code></pre></div><p>我们反编译这个data class Games 来看看真相吧</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">package</span> com<span class="token punctuation">.</span>littonishir<span class="token punctuation">.</span>github<span class="token punctuation">;</span>

<span class="token keyword">import</span> com<span class="token punctuation">.</span>littonishir<span class="token punctuation">.</span>github<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Ishir<span class="token punctuation">;</span>
<span class="token keyword">import</span> kotlin<span class="token punctuation">.</span>Metadata<span class="token punctuation">;</span>
<span class="token keyword">import</span> kotlin<span class="token punctuation">.</span>jvm<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>Intrinsics<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>jetbrains<span class="token punctuation">.</span>annotations<span class="token punctuation">.</span>NotNull<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Ishir</span>
<span class="token annotation punctuation">@Metadata</span><span class="token punctuation">(</span>
   mv <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
   bv <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
   k <span class="token operator">=</span> <span class="token number">1</span>
<span class="token punctuation">)</span>
<span class="token comment">//final修饰符已经没有了</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Games</span> <span class="token punctuation">{</span>
   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">private</span> <span class="token keyword">final</span> String name<span class="token punctuation">;</span>
   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">private</span> <span class="token keyword">final</span> String version<span class="token punctuation">;</span>

   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">public</span> String <span class="token function">getVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>version<span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token keyword">public</span> <span class="token function">Games</span><span class="token punctuation">(</span><span class="token annotation punctuation">@NotNull</span> String name<span class="token punctuation">,</span> <span class="token annotation punctuation">@NotNull</span> String version<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      Intrinsics<span class="token punctuation">.</span><span class="token function">checkParameterIsNotNull</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      Intrinsics<span class="token punctuation">.</span><span class="token function">checkParameterIsNotNull</span><span class="token punctuation">(</span>version<span class="token punctuation">,</span> <span class="token string">&quot;version&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>version <span class="token operator">=</span> version<span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">public</span> <span class="token keyword">final</span> String <span class="token function">component1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">public</span> <span class="token keyword">final</span> String <span class="token function">component2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">public</span> <span class="token keyword">final</span> Games <span class="token function">copy</span><span class="token punctuation">(</span><span class="token annotation punctuation">@NotNull</span> String name<span class="token punctuation">,</span> <span class="token annotation punctuation">@NotNull</span> String version<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      Intrinsics<span class="token punctuation">.</span><span class="token function">checkParameterIsNotNull</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      Intrinsics<span class="token punctuation">.</span><span class="token function">checkParameterIsNotNull</span><span class="token punctuation">(</span>version<span class="token punctuation">,</span> <span class="token string">&quot;version&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Games</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> version<span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token comment">// $FF: synthetic method</span>
   <span class="token comment">// $FF: bridge method</span>
   <span class="token annotation punctuation">@NotNull</span>
   <span class="token keyword">public</span> <span class="token keyword">static</span> Games copy$<span class="token keyword">default</span><span class="token punctuation">(</span>Games var0<span class="token punctuation">,</span> String var1<span class="token punctuation">,</span> String var2<span class="token punctuation">,</span> <span class="token keyword">int</span> var3<span class="token punctuation">,</span> Object var4<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>var4 <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>
         <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">UnsupportedOperationException</span><span class="token punctuation">(</span><span class="token string">&quot;Super calls with default arguments not supported in this target, function: copy&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
         <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>var3 <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            var1 <span class="token operator">=</span> var0<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>

         <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>var3 <span class="token operator">&amp;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            var2 <span class="token operator">=</span> var0<span class="token punctuation">.</span><span class="token function">getVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>

         <span class="token keyword">return</span> var0<span class="token punctuation">.</span><span class="token function">copy</span><span class="token punctuation">(</span>var1<span class="token punctuation">,</span> var2<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
   <span class="token punctuation">}</span>

   <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token string">&quot;Games(name=&quot;</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;, version=&quot;</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;)&quot;</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      String var10000 <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">int</span> var1 <span class="token operator">=</span> <span class="token punctuation">(</span>var10000 <span class="token operator">!=</span> null <span class="token operator">?</span> var10000<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">31</span><span class="token punctuation">;</span>
      String var10001 <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> var1 <span class="token operator">+</span> <span class="token punctuation">(</span>var10001 <span class="token operator">!=</span> null <span class="token operator">?</span> var10001<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>

   <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">equals</span><span class="token punctuation">(</span>Object var1<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token operator">!=</span> var1<span class="token punctuation">)</span> <span class="token punctuation">{</span>
         <span class="token keyword">if</span> <span class="token punctuation">(</span>var1 <span class="token keyword">instanceof</span> <span class="token class-name">Games</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            Games var2 <span class="token operator">=</span> <span class="token punctuation">(</span>Games<span class="token punctuation">)</span>var1<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>Intrinsics<span class="token punctuation">.</span><span class="token function">areEqual</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> var2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> Intrinsics<span class="token punctuation">.</span><span class="token function">areEqual</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> var2<span class="token punctuation">.</span><span class="token function">getVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
               <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
         <span class="token punctuation">}</span>

         <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
         <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
   <span class="token punctuation">}</span>
<span class="token comment">//无参构造诞生</span>
   <span class="token keyword">public</span> <span class="token function">Games</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="内部类"><a href="#内部类" aria-hidden="true" class="header-anchor">#</a> 内部类</h3> <br> <ul><li>定义在类内部的类</li> <li>默认是静态内部类,非静态用inner 关键字标记</li></ul> <p><strong>inner</strong></p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">open</span> <span class="token keyword">class</span> Outter<span class="token punctuation">{</span>
    <span class="token keyword">val</span> a<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">0</span>

    <span class="token keyword">inner</span> <span class="token keyword">class</span> Inner<span class="token punctuation">{</span>
        <span class="token keyword">val</span> a<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">5</span>
        <span class="token keyword">fun</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">/**
             * 静态内部类不持有外部类的引用,不能访问外部类的成员
             * 非静态内部类持有外部类的引用,可以访问外部类的成员
             */</span>
            <span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
            <span class="token comment">//完整的写法</span>
            <span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token label symbol">@Outter</span><span class="token punctuation">.</span>a<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>匿名内部类</p> <ul><li>没有定义名字的内部类</li> <li>类名编译时生成,类似于Outter$1.class</li> <li>可以继承父类实现多个接口,Java不可以继承父类在这里</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>	<span class="token comment">//匿名内部类的写法</span>
    <span class="token keyword">val</span> textView <span class="token operator">=</span> <span class="token function">TextView</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span>
    textView<span class="token punctuation">.</span><span class="token function">setOnClickListener</span> <span class="token punctuation">{</span><span class="token keyword">object</span> <span class="token operator">:</span> View<span class="token punctuation">.</span><span class="token function">OnClickListener</span> <span class="token punctuation">{</span>
                    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">onClick</span><span class="token punctuation">(</span>view<span class="token operator">:</span> View<span class="token operator">?</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        textView<span class="token punctuation">.</span>text <span class="token operator">=</span> <span class="token string">&quot;hi&quot;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
    <span class="token comment">//lambda的写法</span>
	textview<span class="token punctuation">.</span><span class="token function">setOnClickListener</span> <span class="token punctuation">{</span> textview<span class="token punctuation">.</span>text <span class="token operator">=</span> <span class="token string">&quot;hi&quot;</span> <span class="token punctuation">}</span>

</code></pre></div><br> <h3 id="枚举"><a href="#枚举" aria-hidden="true" class="header-anchor">#</a> 枚举</h3> <br> <ul><li>实例可数的类,注意枚举也是类</li> <li>可以修改构造,添加成员</li> <li>可以提升代码的表现力,也有一定的性能开销</li></ul> <p><strong>enum</strong></p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">enum</span> <span class="token keyword">class</span> <span class="token function">LogLevel</span><span class="token punctuation">(</span><span class="token keyword">val</span> id<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">VERBOSE</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">DEBUG</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">INFO</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">WARN</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">ERROR</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">ASSERT</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">fun</span> <span class="token function">getTag</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> String <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token string">&quot;<span class="token interpolation variable">$id</span>, <span class="token interpolation variable">$name</span>&quot;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> String <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token string">&quot;<span class="token interpolation variable">$name</span>, <span class="token interpolation variable">$ordinal</span>&quot;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token operator">&lt;</span>String<span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">println</span><span class="token punctuation">(</span>LogLevel<span class="token punctuation">.</span>DEBUG<span class="token punctuation">.</span>ordinal<span class="token punctuation">)</span>
            LogLevel<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token operator">::</span>println<span class="token punctuation">)</span>
            <span class="token function">println</span><span class="token punctuation">(</span>LogLevel<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token string">&quot;ERROR&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="密封类"><a href="#密封类" aria-hidden="true" class="header-anchor">#</a> 密封类</h3> <br> <ul><li>子类可数</li> <li>1.1之前子类必须定义在密封类的内部</li> <li>1.1之后子类只需要与密封类在同一个文件中</li></ul> <p><strong>sealed</strong></p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">sealed</span> <span class="token keyword">class</span> PlayerCmd <span class="token punctuation">{</span>
    <span class="token keyword">class</span> <span class="token function">Play</span><span class="token punctuation">(</span><span class="token keyword">val</span> url<span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token keyword">val</span> position<span class="token operator">:</span> Long <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token function">PlayerCmd</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">class</span> <span class="token function">Seek</span><span class="token punctuation">(</span><span class="token keyword">val</span> position<span class="token operator">:</span> Long<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token function">PlayerCmd</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">object</span> Pause<span class="token operator">:</span> <span class="token function">PlayerCmd</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">object</span> Resume<span class="token operator">:</span> <span class="token function">PlayerCmd</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">object</span> Stop<span class="token operator">:</span> <span class="token function">PlayerCmd</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">enum</span> <span class="token keyword">class</span> PlayerState<span class="token punctuation">{</span>
    IDLE<span class="token punctuation">,</span> PAUSE<span class="token punctuation">,</span> PLAYING
<span class="token punctuation">}</span>
</code></pre></div></div></article> <!----></div></div></div> <center><hr width="0%"> <span id="subtitle">&quot;Someday&quot;sounds a lot like the thing people say when they actually mean &quot;Never&quot;. </span> <div class="row"><div class="offset-lg-2 col-lg-8 offset-md-1 col-md-10"><p class="small">© 2019 litton ishir. Code released under the <a href="https://opensource.org/licenses/MIT" target="_blank">MIT License</a></p></div></div></center></div></div>
    <script src="/assets/js/app.b387b3a3.js" defer></script><script src="/assets/js/6.0cda6858.js" defer></script>
  </body>
</html>
